package adv8.server1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author oldliu
 * @since 1.0
 */
public class FileServer {
    public static void main(String[] args) {
        ServerSocket st = null;
        //线程池
        ExecutorService es = Executors.newFixedThreadPool(20);
        try {
            st = new ServerSocket(8889);
            System.out.println("文件服务器启动。。。");
            while (true) {
                final Socket s = st.accept();
                //submit
                es.execute(new FileThread(s));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

class FileThread implements Runnable {
    private Socket s;
    public FileThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        final String PIC="D:\\Documents\\图片和资料\\job-salary.png";


        System.out.println(s + "连接上服务器了，准备聊天");
        try {
            //1：读取文件
            FileInputStream fin=new FileInputStream(PIC);
            byte []b=new byte[fin.available()];
            int len=fin.read(b);
            fin.close();
            //InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            out.write(b,0,len);
            out.flush();
            System.out.println("文件发送给："+s+"完成");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
                System.out.println(s + "关闭连接");
            } catch (Exception e) {

            }
        }
    }
}
